﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.LogicComponents {
    import Engine.Classes.*;
    import Engine.Managers.*;
    import Display.DialogUI.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Classes.sim.*;
    import Classes.effects.*;
    import Display.*;
    import Classes.gates.*;
    import Modules.crew.*;
    import Display.RenameUI.*;
    import Modules.guide.actions.*;

    public class MunicipalComponentBase {

        protected static const UPGRADE_INTRO_DELAY:int = 2000;
        protected static const TOASTER_SAM:String = "assets/dialogs/toasterSam.png";

        protected static var m_gateFactory:GateFactory;

        protected var m_municipal:Municipal;
        protected var m_gate:IGate;
        protected var m_lastPopulation:int;
        protected var m_inExperiment:Boolean = true;
        protected var m_cancelTutorial:Boolean = false;

        public function MunicipalComponentBase(_arg1:Municipal){
            this.m_municipal = _arg1;
            if (m_gateFactory == null){
                m_gateFactory = new GateFactory();
                m_gateFactory.register(GateType.INVENTORY, InventoryGate);
                m_gateFactory.register(GateType.CREW, CrewGate);
            };
            this.m_gate = m_gateFactory.loadGateFromXML(this.m_municipal.getItem(), this.m_municipal, "pre_upgrade", this.onUpgradeSuccess);
        }
        public function passesExperimentGate():Boolean{
            return (this.m_inExperiment);
        }
        public function onLoadObject(_arg1:Object):void{
            if (this.m_gate != null){
                this.m_gate.targetObjectId = this.m_municipal.getId();
            };
        }
        public function onSetId(_arg1:Number):void{
            if (this.m_gate != null){
                this.m_gate.targetObjectId = _arg1;
            };
        }
        public function getProgressBarEndFunction():Function{
            return (null);
        }
        public function handlePlayAction():void{
            var _local1:Number;
            var _local2:String;
            var _local3:RenameDialog;
            if (Global.guide.isActive()){
                RollCallManager.debugSample("MunicipalComponentBase", "guideActiveClick");
            };
            if (!Global.guide.isActive()){
                if (((!(Global.isVisiting())) && ((this.m_municipal.getState() == HarvestableResource.STATE_GROWN)))){
                    _local1 = this.m_municipal.getItem().harvestEnergyCost;
                    if (!Global.player.checkEnergy(-(_local1))){
                        this.m_municipal.displayStatus(ZLoc.t("Main", "NotEnoughEnergyRed"));
                    } else {
                        this.m_municipal.doEnergyChanges(-(_local1), new Array("energy", "expenditures", "collect_municipal", this.m_municipal.getItem().name));
                        Global.player.heldEnergy = (Global.player.heldEnergy + _local1);
                        this.m_municipal.heldEnergy = _local1;
                        Global.world.citySim.pickupManager.enqueue("NPC_mailman", this.m_municipal);
                    };
                } else {
                    if (((!(Global.isVisiting())) && (this.m_municipal.isUpgradePossible()))){
                        this.m_municipal.upgradeBuildingIfPossible();
                    } else {
                        if (this.m_municipal.getItem().special_onclick == "visitor_center"){
                            if (Global.isVisiting()){
                                StatsManager.sample(1000, "dialog", "visitorcenter", "view", "visitorboard");
                            } else {
                                StatsManager.sample(1000, "dialog", "visitorcenter", "view", "ownboard");
                            };
                            UI.displayVisitorCenterDialog();
                        } else {
                            if (this.m_municipal.getItem().special_onclick == "clerk_office"){
                                if (!Global.isVisiting()){
                                    _local2 = ZLoc.t("Dialogs", "RenameUI_message");
                                    _local3 = new RenameDialog(_local2, "RenameUI", GenericDialogView.TYPE_OK, null, "RenameUI");
                                    Global.ui.renameDialog = _local3;
                                    UI.displayPopup(_local3);
                                };
                            };
                        };
                    };
                };
            } else {
                if (((this.isUpgradePossible()) && ((Global.guide.getActiveSequence().currentAction() is GAWaitForClickAnywhere)))){
                    if (this.m_gate != null){
                        if (this.passesExperimentGate()){
                            this.m_gate.displayGate();
                        };
                    };
                };
            };
        }
        public function doHarvestDropOff(_arg1:Boolean):void{
            if (!this.passesExperimentGate()){
                return;
            };
            if (((!(this.m_municipal.getItem().upgrade)) || (!(this.m_municipal.getItem().upgrade.isValid())))){
                return;
            };
            if (this.m_municipal.getItem().upgrade.requirements.isRequirementTypeDefined(Requirements.REQUIREMENT_UPGRADE_ACTIONS)){
                this.m_municipal.incrementUpgradeActionCount();
            };
        }
        public function getToolTipStatus():String{
            if (((!(Global.isVisiting())) && ((this.m_municipal.getState() == HarvestableResource.STATE_PLANTED)))){
                return (ZLoc.t("Main", "BareResidence", {time:GameUtil.formatMinutesSeconds(this.m_municipal.getGrowTimeLeft())}));
            };
            return ("");
        }
        public function getToolTipAction():String{
            var _local1:String = this.m_municipal.getGameModeToolTipAction();
            if (!_local1){
                if (((this.isHarvestable()) && (!(Global.guide.isActive())))){
                    _local1 = ZLoc.t("Main", "RipeEntertainment");
                } else {
                    if (((this.isUpgradePossible()) && (this.passesExperimentGate()))){
                        _local1 = ZLoc.t("Dialogs", "UpgradeToolTip");
                    };
                };
            };
            return (_local1);
        }
        protected function isUpgradePossible():Boolean{
            return (((!(Global.isVisiting())) && (this.m_municipal.isUpgradePossible())));
        }
        public function enableUpdateArrow():Boolean{
            return (((!(Global.isVisiting())) && ((((this.m_municipal.getState() == HarvestableResource.STATE_GROWN)) || (((this.m_municipal.isUpgradePossible()) && (!(this.m_municipal.isLocked()))))))));
        }
        public function createStagePickEffect():void{
            if (Global.guide.isActive()){
                return;
            };
            if (this.isHarvestable()){
                this.createStagePickEffectHelper(StagePickEffect.PICK_1);
            } else {
                if (((((this.passesExperimentGate()) && (this.isUpgradePossible()))) && (!(this.m_municipal.isLocked())))){
                    if (((!((this.m_gate == null))) && (this.m_gate.checkForKeys()))){
                        this.createStagePickEffectHelper(StagePickEffect.PICK_UPGRADE_HAMMER);
                    } else {
                        if (this.m_municipal.getShowUpgradeArrow()){
                            this.createStagePickEffectHelper(StagePickEffect.PICK_UPGRADE);
                        };
                    };
                };
            };
        }
        protected function createStagePickEffectHelper(_arg1:String):void{
            var _local2:StagePickEffect = this.m_municipal.stagePickEffect;
            if (!_local2){
                _local2 = (MapResourceEffectFactory.createEffect(this.m_municipal, EffectType.STAGE_PICK) as StagePickEffect);
                _local2.setPickType(_arg1);
                _local2.float();
            } else {
                _local2.setPickType(_arg1);
                _local2.reattach();
                _local2.float();
            };
            this.m_municipal.stagePickEffect = _local2;
        }
        public function upgradeBuildingIfPossible():void{
            if (!this.m_municipal.isUpgradePossible()){
                return;
            };
            if (this.m_gate != null){
                if (this.passesExperimentGate()){
                    this.m_cancelTutorial = true;
                    this.m_gate.displayGate();
                };
            } else {
                this.onUpgradeSuccess();
            };
        }
        protected function onUpgradeSuccess():void{
            var _local1:Item = Global.gameSettings.getItemByName(this.m_municipal.getItem().upgrade.newItemName);
            GameTransactionManager.addTransaction(new TUpgradeBuilding(this.m_municipal));
            this.m_municipal.onUpgrade(this.m_municipal.getItem(), _local1);
            this.doUpgradeAnimations();
            this.m_gate = m_gateFactory.loadGateFromXML(this.m_municipal.getItem(), this.m_municipal, "pre_upgrade", this.onUpgradeSuccess);
            Global.world.citySim.recomputePopulationCap(Global.world);
            this.m_municipal.resetUpgradeActionCount();
        }
        protected function isHarvestable():Boolean{
            return (((!(Global.isVisiting())) && ((this.m_municipal.getState() == HarvestableResource.STATE_GROWN))));
        }
        public function doUpgradeAnimations():void{
            this.m_municipal.doUpgradeAnimations();
        }
        public function onPopulationInit(_arg1:int, _arg2:int, _arg3:int):void{
        }
        public function onPopulationChanged(_arg1:int, _arg2:int, _arg3:int, _arg4:int):void{
        }
        public function onLevelChanged(_arg1:int):void{
        }
        public function onBuildingConstructionCompleted():void{
        }
        protected function isEqualOrHigherUpgradedMunicipal(_arg1:WorldObject):Boolean{
            var _local2:Item;
            if ((_arg1 is MapResource)){
                _local2 = (_arg1 as MapResource).getItem();
                return (((((((!((_local2 == null))) && ((_local2.type == "municipal")))) && ((_local2.behavior == this.m_municipal.getItem().behavior)))) && ((_local2.level >= this.m_municipal.getItem().level))));
            };
            return (false);
        }

    }
}//package Classes.LogicComponents 
